library _;

import 'package:flutter/material.dart';
import 'package:flutter_app/Const/IFinal.dart';
import 'package:flutter_app/Extension/String++.dart';
import 'package:flutter_app/Model/CartModel.dart';
import 'package:flutter_app/Model/Enums.dart';
import 'package:flutter_app/Page/Home/OrderConfirm/main.dart';
import 'package:flutter_app/Util/App/AppUtils.dart';
import 'package:flutter_app/Util/Object++.dart';
import 'package:flutter_app/View/MyNumPicker.dart';
import 'package:flutter_app/View/MyRefresher.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:flutter_yfree/YFree.dart';

part 'controller.dart';

part 'view.dart';

class CartPage extends YPage {
  CartPage({
    required this.isSinglePage,
    this.onCreate,
  });

  MyRefresherController? myRefresherController;
  Function(CartPageController controller)? onCreate;

  bool isSinglePage;
  CartListModel? model_carts;
  var page = 1;

  @override
  void yOnCreate() {
    onCreate?.call(CartPageController(this));
    loadData(false);
  }

  @override
  void yOnDestory() {
    myRefresherController?.dispose();
  }

  @override
  bool ySafeAreaBottom() => isSinglePage;

  @override
  Widget yBuild(BuildContext context) {
    return Container(
      color: IFinal.Colors.pageBG,
      child: Column(
        children: [
          YAppBar(title: "购物车(${model_carts?.count_carts ?? "···"})", canBack: isSinglePage),
          Expanded(
            child: Column(
              children: [
                Expanded(
                  child: MyRefresher(
                    emptyTitle: "购物车还是空的～",
                    emptyImage: "images/bg_empty_cart.png",
                    onCreated: (controller) => myRefresherController = controller,
                    onPull: (controller, state) => loadData(state == MyRefresherState.Up),
                    type: MyRefresherType.Down,
                    child: ListView.builder(
                      primary: false,
                      shrinkWrap: true,
                      padding: EdgeInsets.only(bottom: 10),
                      itemCount: model_carts?.items_carts.length ?? 0,
                      itemBuilder: (context, index) => view_item(model_carts!.items_carts[index]),
                    ),
                  ),
                ),
                view_bottom(),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class CartPageController {
  CartPageController(this.page);

  CartPage page;

  void loadData() {
    page.loadData(false);
  }
}
